﻿using System.Web.Mvc;


namespace Resto.CostControl.WebControl.DWZAdapter
{
    public class DWZPageModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (DWZPageInfo)(bindingContext.Model ?? new DWZPageInfo());
            var dict = controllerContext.HttpContext.Request.Params;

            int page;
            if (int.TryParse(dict["pagepageNum"], out page))
            {
                model.PageIndex = page - 1;
            }

            int rows;
            if (int.TryParse(dict["numPerPage"], out rows))
            {
                model.PageSize = rows;
            }
            else
            {
                model.PageSize = 20;
            }

            string sort = dict["orderField"];
            if (!string.IsNullOrEmpty(sort))
            {
                model.SortName = sort;
            }
            else
            {
                model.SortName = "Id";
            }

            string order = dict["orderDirection"];
            if (!string.IsNullOrEmpty(order))
            {
                model.SortOrder = order;
            }
            else
            {
                model.SortOrder = "asc";
            }

            string keywords = dict["keywords"];
            if (!string.IsNullOrEmpty(keywords))
            {
                model.Keywords = keywords;
            }

            string searchData = dict["searchData"];
            if (!string.IsNullOrEmpty(searchData))
            {
                model.SearchData = searchData;
            }

            return model;
        }
    }
}
